package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import controller.ContactsControllerListener;
import po.Room;
import po.Student;

/**
 * @author 27261
 *总览对话框
 */
public class OverviewDialog extends JDialog{
	private static final long serialVersionUID = 1L;
	private JPanel tablePanel;
	private ContactsControllerListener controllerListener;
	private int selRow = -1;//选中的表格行索引
	private long studentId;
	private int roomId;
	private JPopupMenu rightClickMenu;
	private JMenuItem updateItem;
	private JMenuItem deleteItem;
	private JMenuItem copyItem;
	private JPanel panel;
	public OverviewDialog(ContactsFrame owner,String title) {
		controllerListener = new ContactsControllerListener();
		this.controllerListener = owner.getControllerListener();  //让RoomFrame和studentAddDialog共用同一个controller对象
		this.controllerListener.setOverViewDialog(this);
		this.setTitle(title);
		this.setModal(true);
		this.setResizable(false);
		this.setBounds(1000,750,1000,750);
		this.setLocationRelativeTo(null);
		this.panel = null;
		if("通讯录".equals(this.getTitle())) {
			panel = getStudentTablePanel(controllerListener.getContactsService().findAllStudent());
			this.add(panel);
		}
		else {
			panel = getRoomTablePanel(controllerListener.getContactsService().findAllRoom());
			this.add(panel);
		}
		this.validate();
		this.setVisible(true);
		
	}
	private JPanel getStudentTablePanel(List<Student> studentList) {
		tablePanel = new JPanel();
		rightClickMenu = new JPopupMenu();
		updateItem = new JMenuItem();
		updateItem.setText("修改");
		updateItem.addActionListener(controllerListener);
		updateItem.setActionCommand("updateDialog");
		deleteItem = new JMenuItem();
		deleteItem.setText("删除");
		deleteItem.addActionListener(controllerListener);
		deleteItem.setActionCommand("delete");
		copyItem = new JMenuItem();
		copyItem.setText("复制");
		copyItem.addActionListener(controllerListener);
		copyItem.setActionCommand("copyStudent");
		rightClickMenu.add(copyItem);
		rightClickMenu.add(updateItem);
		rightClickMenu.add(deleteItem);
		String titles[] = {"姓名", "学号", "宿舍", "生日", "手机号码", "邮箱", "住址", "老家" };
		Object data[][] = new Object[studentList.size()][8];
		Iterator<Student> iter = studentList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Student student = iter.next();
			data[i][0] = student.getName();
			data[i][1] = student.getStudentId();
			data[i][2] = student.getRoom().getRoom_name();
			data[i][3] = student.getBirthday();
			data[i][4] = student.getPhoneNumber();
			data[i][5] = student.getEmail();
			data[i][6] = student.getAddress();
			data[i][7] = student.getNative_place();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data,titles);
		JTable table = new JTable(tableModel);
		table.setPreferredScrollableViewportSize(new Dimension(900, 600));
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14)); //表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 50)); //表头高度
		table.setRowHeight(table.getRowHeight()+30); //表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
		table.repaint();
		tablePanel.add(new JScrollPane(table));
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				int mods=e.getModifiers();
				table.add(rightClickMenu);
				//鼠标右键
				if((mods&InputEvent.BUTTON3_MASK)!=0){
					//弹出菜单
					rightClickMenu.show(table, e.getX(), e.getY());
				}
				selRow = table.getSelectedRow();  //得到选中的行
				String name = table.getValueAt(selRow, 0).toString().trim();
				studentId = (long)table.getValueAt(selRow, 1);
				String roomName = (String)table.getValueAt(selRow, 2);
				String date = table.getValueAt(selRow, 3).toString().trim();
				String phoneNum = table.getValueAt(selRow, 4).toString().trim();
				String email = table.getValueAt(selRow, 5).toString().trim();
				String address = table.getValueAt(selRow, 6).toString().trim();
				String nativePlace = table.getValueAt(selRow, 7).toString().trim();
				System.out.println("姓名："+name+";学号："+studentId+";宿舍："+roomName+";生日："+date+";手机号码："+phoneNum+";邮箱："+email+";住址："+address+";老家：" +nativePlace);
			}
		});
		return tablePanel;
	}
	private JPanel getRoomTablePanel(List<Room> roomList) {
		tablePanel = new JPanel();
		rightClickMenu = new JPopupMenu();
		updateItem = new JMenuItem();
		updateItem.setText("修改");
		updateItem.addActionListener(controllerListener);
		updateItem.setActionCommand("updateRoomDialog");
		deleteItem = new JMenuItem();
		deleteItem.setText("删除");
		deleteItem.addActionListener(controllerListener);
		deleteItem.setActionCommand("deleteRoom");
		copyItem = new JMenuItem();
		copyItem.setText("复制");
		copyItem.addActionListener(controllerListener);
		copyItem.setActionCommand("copyRoom");
		rightClickMenu.add(copyItem);
		rightClickMenu.add(updateItem);
		rightClickMenu.add(deleteItem);
		String titles[] = {"宿舍编号", "宿舍名"};
		Object data[][] = new Object[roomList.size()][2];
		Iterator<Room> iter = roomList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Room room = iter.next();
			data[i][0] = room.getRoom_id();
			data[i][1] = room.getRoom_name();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data,titles);
		JTable table = new JTable(tableModel);
		table.setPreferredScrollableViewportSize(new Dimension(900, 600));
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14)); //表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 50)); //表头高度
		table.setRowHeight(table.getRowHeight()+30); //表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
		table.repaint();
		tablePanel.add(new JScrollPane(table));
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				int mods=e.getModifiers();
				table.add(rightClickMenu);
				//鼠标右键
				if((mods&InputEvent.BUTTON3_MASK)!=0){
					//弹出菜单
					rightClickMenu.show(table, e.getX(), e.getY());
				}
				selRow = table.getSelectedRow();  //得到选中的行
				roomId = (int)table.getValueAt(selRow, 0);
				String roomName = table.getValueAt(selRow, 1).toString().trim();
				System.out.println("宿舍编号:"+roomId+"宿舍名:"+roomName);
			}
		});
		return tablePanel;
	}
	/**
	 * 重新加载通讯录数据表格面板
	 */
	public void studentRefresh(List<Student> studentList) {
		remove(panel);
		panel = getStudentTablePanel(controllerListener.getContactsService().findAllStudent());
		this.add(panel);
		this.validate();
	}
	/**
	 * 重新加载通讯录数据表格面板
	 */
	public void roomRefresh(List<Room> roomList) {
		remove(panel);
		panel = getRoomTablePanel(controllerListener.getContactsService().findAllRoom());
		this.add(panel);
		this.validate();
	}
	public long getStudentId() {
		return studentId;
	}
	public int getRoomId() {
		return roomId;
	}
	public int getSelRow() {
		return selRow;
	}
	public ContactsControllerListener getControllerListener() {
		return controllerListener;
	}
}
